/**
 * 
 */
package bancosys.tec.security.impl.domain;

import java.util.ArrayList;
import java.util.Collection;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import jmine.tec.persist.annotation.Alias;
import jmine.tec.persist.annotation.UniqueConstraint;
import jmine.tec.persist.annotation.UniqueConstraints;

import org.apache.commons.lang.StringUtils;

import bancosys.tec.component.Documentation;
import bancosys.tec.persist.authorization.annotation.DisplayName;
import bancosys.tec.persist.bussobj.PersistableBusinessObject;
import bancosys.tec.persist.persister.annotation.NaturalKey;

/**
 * @created 14 setembro 2010
 * @author seiti
 */
@Entity
@Table(name = "META_CREDENTIAL")
@Alias("MTCRED")
@SequenceGenerator(name = "SEQ_MTCRED", sequenceName = "SEQ_MTCRED")
@Documentation("ARMAZENA METACREDENTIALS QUE AGRUPAM CREDENCIAIS")
@UniqueConstraints({ @UniqueConstraint(name = "AK_MTCRED_0", columns = { "NAME" }) })
public class MetaCredential extends PersistableBusinessObject {

    private Long id;

    private String name;

    private String description;

    private Collection<Credential> credentials = new ArrayList<Credential>();

    /**
     * @return the id
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_MTCRED")
    @Column(name = "COD_META_CREDENTIAL", nullable = false)
    @Documentation("CODIGO DA META CREDENCIAL")
    public Long getId() {
        return this.id;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @return the name
     */
    @NaturalKey
    @Column(name = "NAME", nullable = false)
    @Documentation("NOME DA CREDENCIAL")
    @DisplayName(value = "Meta Credencial")
    public String getName() {
        return this.name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the description
     */
    @Column(name = "DESCRIPTION")
    @Documentation("DESCRICAO DA CREDENCIAL")
    public String getDescription() {
        return this.description;
    }

    /**
     * @param description the description to set
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * @param credentials the credentials to set
     */
    public void setCredentials(Collection<Credential> credentials) {
        this.credentials = credentials;
    }

    /**
     * @return the credentials
     */
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "metaCredential")
    @JoinColumn(name = "COD_META_CREDENTIAL")
    public Collection<Credential> getCredentials() {
        return this.credentials;
    }

    /**
     * Adiciona uma credential
     * 
     * @param credential credential
     */
    public void addCredential(Credential credential) {
        this.getCredentials().add(credential);
        credential.setMetaCredential(this);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        if (StringUtils.isNotBlank(this.getDescription())) {
            return this.getDescription();
        }
        return this.getName();
    }
}